import sys

class pywslib:
	# struttura di argv = [0] = file [1] = GET [2] = POST
	GET = {}
	POST = {}
	COOKIE = {}
	Header = ""
	cookies = 0
	ctype = "text/html"
	def __init__(self):
		arguments = []
		for arg in sys.argv:
			arguments.append(arg.replace("%39;","'"))
		try:
			get_p = arguments[1].split("&")
			for get_a in get_p:
				garg = get_a.split("=")
				self.GET[garg[0]] = garg[1]
		except:
			pass
		try:
			post_p = arguments[2].split("&")
			for post_a in post_p:
				parg = post_a.split("=")
				self.POST[parg[0]] = parg[1]
		except:
			pass
		try:
			biscotti = arguments[3].split("; ")
			for biscotto in biscotti:
				bisc = biscotto.split("=")
				self.COOKIE[bisc[0]] = bisc[1].rstrip()
		except:
			pass
			
		
	def setcookie(self,name,value):
		if not self.cookies:
			self.cookies = str(name)+"="+str(value)
		else:
			self.cookies = "; "+str(name)+"="+str(value)
	def setctype(self,ctype):
		self.ctype = ctype
	
	def header(self):
		if self.cookies:
			self.Header = "HTTP/1.1 200 OK\r\nCache-control: private\r\nContent-Type: "+str(self.ctype)+"\r\nSet-Cookie: "+str(self.cookies)+"\r\n\r\n"
		else:
			self.Header = "HTTP/1.1 200 OK\r\nCache-control: private\r\nContent-Type: "+str(self.ctype)+"\r\n\r\n"
		print self.Header
